package client_tests;

import net.es.lookup.client.QueryClient;
import net.es.lookup.client.SimpleLS;
import net.es.lookup.common.ReservedKeys;
import net.es.lookup.common.exception.LSClientException;
import net.es.lookup.common.exception.ParserException;
import net.es.lookup.common.exception.QueryException;
import net.es.lookup.queries.Query;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * User: luke
 * Date: 8/5/13
 * Time: 4:07 PM
 * To change this template use File | Settings | File Templates.
 */
public class SerialQuery extends SerialTest implements ScalabilityTest {

    public SerialQuery() {

        super();
    }

    @Override
    public void initialize(String targetHost, int port, String sourceHost, int op_interval, int ignored, int testRounds) throws LSClientException {

        opCodeBase = sourceHost + ":";
        this.targetHost = targetHost;
        this.port = port;
        rx = new ResultTransmitter();
        sls = new SimpleLS(targetHost, port);
        qc = new QueryClient(sls);
        this.sourceHost = sourceHost;
        this.testRounds = testRounds;
    }

    @Override
    public void runTest() {

        int i;
        for (i = 0; i < testRounds; i++) {

            registerTestRecord();

            LinkedList<String> opCodeList = new LinkedList<String>();
            opCodeList.add(opCode);

            String timestamp = null;
            long testStart = 0;
            long testFinish = 0;
            try {

                Query query = new Query();
                query.add(OPCODE, opCodeList);
                DateFormat df = new SimpleDateFormat("MM-dd-HH-mm-ss.SSS");

                testStart = System.currentTimeMillis();
                timestamp = df.format(testStart);
                System.out.println(testStart + ": starting query on: " + opCode);
                qc.setQuery(query);
                qc.query();
                testFinish = System.currentTimeMillis();
                System.out.println(testFinish + ": query finished after " + (testFinish - testStart) / 1000 + " seconds");

                rx.add(sourceHost + timestamp, sls.getConnectionType(), Long.toString(testFinish - testStart) + "ms", Integer.toString(sls.getResponseCode()));


            } catch (LSClientException e) {

                rx.add(sourceHost + timestamp, sls.getConnectionType(),
                        Long.toString(testFinish - testStart), e.getMessage());
            } catch (ParserException e) {

                rx.add(sourceHost + timestamp, sls.getConnectionType(),
                        Long.toString(testFinish - testStart), e.getMessage());
            } catch (QueryException e) {

                rx.add(sourceHost + timestamp, sls.getConnectionType(),
                        Long.toString(testFinish - testStart), e.getMessage());
            }
        }
        clearRecords();
    }

    @Override
    public void printResults() {

        rx.print();
    }
}
